/*
 * Copyright (c) 2001 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 * 	  David Corbin - added method to construct full path name
 */
package net.sourceforge.p4eclipse.core.resources;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import net.sourceforge.p4eclipse.core.PerforceException;
import net.sourceforge.p4eclipse.core.Policy;
import net.sourceforge.p4eclipse.core.api.PerforceUtil;
import net.sourceforge.p4eclipse.core.syncinfo.FolderSyncInfo;
import net.sourceforge.p4eclipse.core.syncinfo.ResourceSyncInfo;
import net.sourceforge.p4eclipse.ui.PerforceUIPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.core.sync.IRemoteResource;

/**
  * @version 	1.0
  * @author		<a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  * @author		<a href="mailto:dcorbin@users.sourceforge.net">David Corbin</a>
  */
public class RemoteFolder extends RemoteResource
{
	private FolderSyncInfo folderSyncInfo;
	private String revision;
	
	public RemoteFolder(RemoteFolder parent, FolderSyncInfo folderSyncInfo, String revision)
	{
		Policy.assertNotNull(folderSyncInfo);
		
		setParent(parent);
		this.folderSyncInfo = folderSyncInfo;
		this.revision = revision;
	}
	
	/**
	 * Gets the name.
	 * @return Returns a String
	 */
	public String getName()
	{
		// XXX TODO: getName in IRemoteResource should only return the name part.
		if (getParent() == null)
			return folderSyncInfo.getDepotFileName();
		else
			return folderSyncInfo.getDepotFileName().substring(getParent().folderSyncInfo.getDepotFileName().length() + 1);
	}
	
	public IPath getLocation()
	{
		try
		{
			// XXX "/ " is a workaround for p4 where problem.
			String localPath = PerforceUIPlugin.getClient().where(folderSyncInfo.getDepotFileName() + "/ ").getLocalFilename(); 
			return new Path(localPath.substring(0, localPath.length() - 2));
		}
		catch (PerforceException e)
		{
			PerforceUIPlugin.log(e.getStatus());
			return null;
		}
	}

	public RemoteFolder[] getFolders()
	{
		try 
		{
			FolderSyncInfo[] subFolders = 
				PerforceUIPlugin.getClient().dirs(folderSyncInfo.getDepotFileName() + "/*");
			
			RemoteFolder[] result = new RemoteFolder[subFolders.length];
			for (int i = 0; i < subFolders.length; i++)
				result[i] = new RemoteFolder(this, subFolders[i], revision);
			
			return result;
		} 
		catch (PerforceException e)
		{
			PerforceUIPlugin.log(e.getStatus())	;
		}
		
		return new RemoteFolder[0];
	}
	
	public RemoteFile[] getFiles()
	{
		try
		{
			ResourceSyncInfo[] files = 
				PerforceUIPlugin.getClient().fstat(folderSyncInfo.getDepotFileName() + "/*");
			
			List result = new ArrayList();
			for (int i = 0; i < files.length; i++)
			{
				if (!files[i].getHeadAction().equals(ResourceSyncInfo.ACTION_DELETE))
					result.add(new RemoteFile(files[i].getDepotFileName(), revision));
			}
			
			return (RemoteFile[])result.toArray(new RemoteFile[0]);
		}
		catch (PerforceException e)
		{
			PerforceUIPlugin.log(e.getStatus());
		}
		
		return new RemoteFile[0];
	}
	
	/*
	 * @see IRemoteResource#getContents(IProgressMonitor)
	 */
	public InputStream getContents(IProgressMonitor progress) 
		throws TeamException
	{
		return null;
	}

	/*
	 * @see IRemoteResource#isContainer()
	 */
	public boolean isContainer()
	{
		return true;
	}

	/*
	 * @see RemoteResource#getSyncInfo()
	 */
	public ResourceSyncInfo getSyncInfo()
	{
		return null;
	}

	public String getRevision()
	{
		return revision;
	}

	/*
	 * @see IRemoteResource#members(IProgressMonitor)
	 */
	public IRemoteResource[] members(IProgressMonitor progress)
		throws TeamException
	{
		List result = new ArrayList();
		result.addAll(Arrays.asList(getFolders()));
		result.addAll(Arrays.asList(getFiles()));
		
		return (IRemoteResource[])result.toArray(new IRemoteResource[0]);
	}
	public String getFullName()
	{
		if (getParent() != null)
			return getParent().getFullName()+"/"+getName();
		return getName();
	}
}
